from noise import snoise2

shape = (100, 100)
octaves = 8
persistence = 1
gap = 1.025  # 空隙


def perlin_noise_2d(x, y, scale=32.0):
    return (snoise2(
        x=x / scale,
        y=y / scale,
        octaves=octaves,
        persistence=persistence,
        lacunarity=gap,
        repeatx=1024,
        repeaty=1024,
        base=42
    ) + 1) * 0.5


# def smooth_step(min_val, max_val, value):
#     """三次平滑过渡函数"""
#     x = max(0.0, min(1.0, (value - min_val) / (max_val - min_val)))
#     return x * x * (3 - 2 * x)

def complex_terrain_noise(x, y, scale=32.0) -> float:
    # 大陆性 - 形成大片陆地和海洋
    continentals = snoise2(
        x=x / (scale * 1.2),
        y=y / (scale * 1.2),
        octaves=6,
        persistence=1.0,
        lacunarity=1.025,
        repeatx=1024,
        repeaty=1024,
        base=42
    )

    if 0.2 <= continentals < 0.4:
        continentals = 0.3 + (continentals - 0.3) * 48.0  # 斜率突增到 3.0
    # elif continentals >= 0.4:
    #     continentals = 5.1 + continentals

    # 侵蚀 - 添加精细细节和河流状图案
    # erosion = snoise2(
    #     x=x / (scale * 0.01),  # Higher frequency for finer details
    #     y=y / (scale * 0.01),
    #     octaves=2,
    #     persistence=0.7,  # Lower persistence for sharper features
    #     lacunarity=1.8,  # Higher lacunarity for more variation
    #     repeatx=1024,
    #     repeaty=1024,
    #     base=12345  # Different seed for variation
    # )
    #
    # if 0.3 <= erosion <= 0.4:
    #     continentals = -1.0

    # 山峰与山谷 - 创造山地地形
    peaks_valleys = snoise2(
        x=x / (scale * 0.4),
        y=y / (scale * 0.4),
        octaves=8,  # 更尖锐的峰值
        persistence=0.65,  # 更低的持久性，实现更清晰的过渡
        lacunarity=2.0,  # 更高的间隙，更多的锯齿状特征
        repeatx=1024,
        repeaty=1024,
        base=67890  # Different seed for variation
    )

    if peaks_valleys <= -0.1:
        continentals = -0.1

    terrain = continentals
    return 0.5 * (terrain + 1.0)  # Clamp to 0-1 range

if __name__ == '__main__':
    print(complex_terrain_noise(0, 20, 120))